package com.zhonghui.wms.service.impl;

import com.zhonghui.wms.domain.vo.WmsInventoryInformationVo;
import com.zhonghui.wms.mapper.WmsInventoryInformationMapper;
import com.zhonghui.wms.service.IWmsInventoryInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 智能仓储WMS-盘点信息Service业务层处理
 *
 * @author zhonghui
 * @date 2022-05-27
 */
@Service
public class WmsInventoryInformationServiceImpl implements IWmsInventoryInformationService
{
    @Autowired
    private WmsInventoryInformationMapper wmsInventoryInformationMapper;

    /**
     * 查询智能仓储WMS-盘点信息
     *
     * @param id 智能仓储WMS-盘点信息主键
     * @return 智能仓储WMS-盘点信息
     */
    @Override
    public WmsInventoryInformationVo selectWmsInventoryInformationById(Long id)
    {
        return wmsInventoryInformationMapper.selectWmsInventoryInformationById(id);
    }

    /**
     * 查询智能仓储WMS-盘点信息列表
     *
     * @param wmsInventoryInformationVo 智能仓储WMS-盘点信息
     * @return 智能仓储WMS-盘点信息
     */
    @Override
    public List<WmsInventoryInformationVo> selectWmsInventoryInformationList(WmsInventoryInformationVo wmsInventoryInformationVo)
    {
        return wmsInventoryInformationMapper.selectWmsInventoryInformationList(wmsInventoryInformationVo);
    }

    @Override
    public List<WmsInventoryInformationVo> selectWmsInventoryInformationNoPageList() {
        return wmsInventoryInformationMapper.selectWmsInventoryInformationNoPageList();
    }

    /**
     * 新增智能仓储WMS-盘点信息
     *
     * @param wmsInventoryInformationVo 智能仓储WMS-盘点信息
     * @return 结果
     */
    @Override
    public int insertWmsInventoryInformation(WmsInventoryInformationVo wmsInventoryInformationVo)
    {
        String documentNo = generateNumber("PD");
        wmsInventoryInformationVo.setDocumentId(documentNo);
        // 判断实际库存是否大于现库存
        if (wmsInventoryInformationVo.getPhysicalInventory() > wmsInventoryInformationVo.getExistingInventory()){
            // 盘盈
            wmsInventoryInformationVo.setInventoryType(Long.parseLong("1"));
        }else {
            // 盘亏
            wmsInventoryInformationVo.setInventoryType(Long.parseLong("0"));
        }
        return wmsInventoryInformationMapper.insertWmsInventoryInformation(wmsInventoryInformationVo);
    }

    private String generateNumber(String number){
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return number+format.format(date);
    }

    /**
     * 修改智能仓储WMS-盘点信息
     *
     * @param wmsInventoryInformation 智能仓储WMS-盘点信息
     * @return 结果
     */
    @Override
    public int updateWmsInventoryInformation(WmsInventoryInformationVo wmsInventoryInformation)
    {
        // 判断实际库存是否大于现库存
        if (wmsInventoryInformation.getPhysicalInventory() > wmsInventoryInformation.getExistingInventory()){
            // 盘盈
            wmsInventoryInformation.setInventoryType(Long.parseLong("1"));
        }else {
            // 盘亏
            wmsInventoryInformation.setInventoryType(Long.parseLong("0"));
        }
        return wmsInventoryInformationMapper.updateWmsInventoryInformation(wmsInventoryInformation);
    }

    /**
     * 批量删除智能仓储WMS-盘点信息
     *
     * @param ids 需要删除的智能仓储WMS-盘点信息主键
     * @return 结果
     */
    @Override
    public int deleteWmsInventoryInformationByIds(Long[] ids)
    {
        return wmsInventoryInformationMapper.deleteWmsInventoryInformationByIds(ids);
    }

    /**
     * 删除智能仓储WMS-盘点信息信息
     *
     * @param id 智能仓储WMS-盘点信息主键
     * @return 结果
     */
    @Override
    public int deleteWmsInventoryInformationById(Long id)
    {
        return wmsInventoryInformationMapper.deleteWmsInventoryInformationById(id);
    }
}
